<?php
// +----------------------------------------------------------------------
// | 套票信息检测
// +----------------------------------------------------------------------
// | Author: TongBC <50249101@qq.com>
// +----------------------------------------------------------------------
// | Date  : 2015/02/03
// +----------------------------------------------------------------------
class PackTicket{
	
	
	
	
	public $childTicketDatas = array();// 子票信息
	public $paymode_continer = array();
	
	public $paymode = 0;// 支付方式
	public $advance = 0;// 套票需要提前多少天购买
	public $section = 0;// 是否存在验证区间，1 存在
	public $relationChildCount = 0;// 关联子票数量
	public $usedate = array();// 套票有效使用日期数组
	public $message = array();// 提示/错误消息记录
	public $attribute = array();// 景区套票属性
	
	public function __construct($lid=0){
		
		if($lid>0) $sales = $this->childTicketData($lid);// 获取子票数据
	}
	
	// 获取关联子票数据
	public function childTicketData($lid){
		// 获取 attribute
		$sql="select l.id,l.attribute,t.ddays,t.pay,t.delaydays,t.order_start,t.order_end from uu_land l left join uu_jq_ticket t on l.id=t.landid where l.id=$lid limit 1";
		$GLOBALS['le']->query($sql);
		$GLOBALS['le']->fetch_assoc();
		$attr_str = $GLOBALS['le']->f('attribute');
		$this->attribute = $attr = json_decode($attr_str, true);
		
		$pack = array(
			'pay'       => $GLOBALS['le']->f('pay'),
			'ddays'     => $GLOBALS['le']->f('ddays'),
			'delaydays' => $GLOBALS['le']->f('delaydays')
		);
		
		// 获取关联子票
		$child_pids = array();
		foreach($attr as $ct) $child_pids[] = $ct['pid'];
		
		$count = $this->relationChildCount = count($child_pids);
		
		$child_pids_s = implode(',', $child_pids);
		$sql="select p.p_name,p.id,t.ddays,t.pay,t.order_start,t.order_end,t.delaytype,t.delaydays from uu_products p left join uu_jq_ticket t on t.pid=p.id where p.apply_limit=1 and p.p_status<6 and p.id in ($child_pids_s) limit $count";
		$GLOBALS['le']->query($sql);
		while($row = $GLOBALS['le']->fetch_assoc())
		{
			$sales[] = $row;
			
		}
		$this->childTicketDatas = $sales;
		return $sales;
	}
	
	
	
	
	// 检查套票是否合法有效
	public function checkEffectivePack(){
		
		
		if($this->relationChildCount!=count($this->childTicketDatas))
		{
			$this->message[] = '子票非所有都可销售';
			return false;// 子票非所有都可销售
		}
			
		
		// 获取套票的有效时间段 如果开始时间大于结束时间，表示无效
		$useDate = $this->useDate($this->childTicketDatas);// 获取时间交集
		if($useDate['sDate']>$useDate['eDate'])
		{
			$this->message[] = '套票的有效时间有误';
			return false; 
		}
		
		// 所有支付方式都必须一直
		// print_r(array_count_values($this->paymode_continer));
		if(count(array_count_values($this->paymode_continer))>1)
		{
			$this->message[] = '支付方式存在不一致';
			return false;
		}
		
		return true;
	}
	
	
	// 获取套票有效验证（游玩）日期区间
	public function useDate(){

		// print_r($this->childTicketDatas);
		
		$this->paymode = $this->childTicketDatas[0]['pay'];// 支付方式
		$this->advance = $this->childTicketDatas[0]['ddays'];// 提前购买天数
		$orderDate = date('Y-m-d 00:00:00');// 下单时间
		
		// 获取最大提前天数
		if(is_null($this->childTicketDatas)){
			return array(
				'sDate'=>'2015-04-02 00:00:00',
				'eDate'=>'2015-04-01 00:00:00',
				'oDate'=>'2015-04-01 00:00:00',
				'mDate'=>'2015-04-01 00:00:00',
				'section'=>0,
			);
		}
		foreach($this->childTicketDatas as $key=>$data){
			if($data['ddays'] > $this->advance)  $this->advance = $data['ddays'];// 提前天数最大值
			$this->paymode = $data['pay'];// 支付方式
			$this->paymode_continer[] = $data['pay'];
		}
		
		// 初始第一个子票信息
		$iniDate = $this->effectiveDateSection($this->childTicketDatas[0], $orderDate, $playDate);
		foreach($this->childTicketDatas as $key=>$data){
			$playDate = date('Y-m-d 00:00:00', time()+($data['ddays'] * 86400));// 使用时间
			
			$date_t = $this->effectiveDateSection($data, $orderDate, $playDate);
			if($key==0) $iniDate = $date_t;
				
			if($date_t['sDate']>$iniDate['sDate']) $iniDate['sDate'] = $date_t['sDate'];
			if($date_t['eDate']<$iniDate['eDate'] && $date_t['section']>0) $iniDate['eDate'] = $date_t['eDate'];
			if($date_t['section']==1){// 计算套票最大下单时间
				if($iniDate['section']==1){
					$iniDate['oDate'] = $iniDate['oDate'] > $date_t['oDate'] ? $date_t['oDate']:$iniDate['oDate'];
					$iniDate['mDate'] = $iniDate['mDate'] > $date_t['mDate'] ? $date_t['mDate']:$iniDate['mDate'];
				}else{
					$iniDate['section'] = 1;
					$iniDate['oDate'] = $date_t['oDate'];
					$iniDate['mDate'] = $date_t['mDate'];
				}	
			}
			// print_r($iniDate);
		}
		if($iniDate['section']==1){
			$iniDate['oDate'] = date('Y-m-d 23:59:59',(strtotime($iniDate['oDate']) - $this->advance * 86400));
		}
		$this->usedate = $iniDate;
		return $iniDate;
	}
	

	
	// 传入子票信息获取子票游玩日期区间
	public function effectiveDateSection(Array $data, $orderDate='', $playDate=''){
		$arr = array();
		if($orderDate=='') $orderDate = date('Y-m-d 00:00:00');// 下单时间
		if($playDate=='') $playDate = date('Y-m-d 00:00:00');// 游玩时间
		if($data['order_start']!='' && $data['order_end']){
			// 提前天数
			if($data['ddays']>0){
				$date_tmp = date('Y-m-d 00:00:00',time()+ $data['ddays'] * 86400);
			}
			
			$arr['sDate'] = ($date_tmp > $data['order_start']) ? $date_tmp:$data['order_start'];
			$arr['eDate'] = $data['order_end'];
			$arr['oDate'] = $data['order_end'];
			$arr['mDate'] = $data['order_end'];
			$arr['section'] = 1;
		}elseif($data['order_start']=='' && $data['order_end']){// 只有结束时间
			$arr['eDate'] = date('Y-m-d 00:00:00');
			$arr['eDate'] = $data['order_end'];
			$arr['oDate'] = $data['order_end'];
			$arr['mDate'] = $data['order_end'];
			$arr['section'] = 1;
		}else{
			if($data['delaytype']==0){// 游玩时间
				if($data['delaydays']==0) $data['delaydays'] = $this->advance;// 当天有效的获取最大提前的那一天
				$arr['sDate'] = $playDate;
				$arr['eDate'] = date('Y-m-d 23:59:59',strtotime($arr['sDate']) + $data['delaydays']*86400);
			}else{
				$arr['sDate'] = date('Y-m-d 00:00:00',strtotime($orderDate) + $data['ddays']*86400);
				$arr['eDate'] = date('Y-m-d 23:59:59',strtotime($arr['sDate']) + ($data['delaydays']-$data['ddays'])*86400);
			}
			$arr['section'] = 0;
			$arr['oDate'] = $arr['eDate'];
			$arr['mDate'] = $arr['mDate'];
		}
		
		return $arr;
	}

	
}
?>